﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace AutoMeasureV2._1
{
    public class creatXml
    {
        public void writeXml(string xmlPath, fileHandle handle)
        {
            XmlTextWriter textWriter = new XmlTextWriter(xmlPath, Encoding.GetEncoding("GB2312"));

            textWriter.Formatting = Formatting.Indented;  //自动缩进
            textWriter.WriteStartDocument(); //写入文档
            //创建根节点
            textWriter.WriteStartElement("自动化试验信息");
            textWriter.WriteStartElement("资源信息");
            textWriter.WriteEndElement(); //结束节点：资源信息
            textWriter.WriteStartElement("试验工步信息");
            textWriter.WriteEndElement(); //结束节点：试验工步信息
         
            textWriter.WriteEndElement(); //结束节点：自动化试验信息

            textWriter.WriteEndDocument();//结束文档写入
            textWriter.Close();   //关闭textWriter

            XmlDocument doc = new XmlDocument();
            doc.Load(xmlPath);
            mesXML(handle, doc);
            stepXML(handle, doc);
            doc.Save(xmlPath);
        }

        /// <summary>
        /// 资源信息写入
        /// </summary>
        /// <param name="handle">包含工步信息的数据集</param>
        /// <param name="doc">写入的xml文件</param>
        private void mesXML(fileHandle handle, XmlDocument doc)
        {
            XmlNode node = doc.SelectSingleNode("自动化试验信息");
            XmlNode nodeMes = node.SelectSingleNode("资源信息");
            if (node == null || nodeMes == null)
                return;
            else
            {
                foreach (var item in handle.source)
                {
                    XmlElement nodes = doc.CreateElement("资源");
                    nodes.SetAttribute("资源编号", item.UseID);
                    nodes.SetAttribute("资源名称", item.UseName);
                    nodes.SetAttribute("对象名称", item.ObjID);
                    nodes.SetAttribute("对象地址", item.ObjAddress);
                    nodes.SetAttribute("对象端口", item.ObjPort);
                    nodes.SetAttribute("数据类型", item.ObjDigType);
                    nodes.SetAttribute("识别码", item.ObjSign);
                    nodeMes.AppendChild(nodes);
                }
            }

        }

        /// <summary>
        /// 工步信息XML写入
        /// </summary>
        /// <param name="handle">包含工步信息的数据集</param>
        /// <param name="doc">写入的xml文件</param>
        private void stepXML(fileHandle handle, XmlDocument doc)
        {
            XmlNode node = doc.SelectSingleNode("自动化试验信息");
            XmlNode nodeStep = node.SelectSingleNode("试验工步信息");
            if (node == null || nodeStep == null)
                return;
            else
            {
                foreach (var item in handle.source)
                {
                    XmlElement nodes = doc.CreateElement(item.UseID);
                    nodes.SetAttribute("资源名称", item.UseName);
                    nodes.SetAttribute("数据类型", item.ObjDigType);
                    nodeStep.AppendChild(nodes);
                    foreach (var nstep in item.Steps)
                    {
                        XmlElement nodestep = doc.CreateElement("工步");
                        nodestep.SetAttribute("序号", nstep.StepNum.ToString());
                        nodestep.SetAttribute("指令时间", nstep.OrderTime.ToString());
                        nodestep.SetAttribute("指令结果", nstep.TargetValue.ToString());
                        nodestep.SetAttribute("动作时间", nstep.ChangeTime.ToString());
                        nodes.AppendChild(nodestep);
                    }
                }
            }
        }


    }
}
